package ca.llsutherland.squash.bootstrap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import ca.llsutherland.squash.facade.FacadeFactory;
import ca.llsutherland.squash.persistence.DatabaseConnectionManager;

public class Bootstrapper {

	private static boolean isDbAlreadyAutoLoaded = false;
	private static Lock lock = new ReentrantLock();

	public static void bootstrap() {

	    if(!isDbAlreadyAutoLoaded){
	        lock.lock();
			isDbAlreadyAutoLoaded = true;
			try {
                DatabaseConnectionManager.getInstance().initialize();
                autoloadData();
                
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
		}
	}

    private static void autoloadData() {
        FacadeFactory.getAutoloadFacade().autoloadCourtStyles();
        FacadeFactory.getAutoloadFacade().autoloadRoles();
    }
	
}
